<template>
	<view class="album-create" :style="{ '--background-image': `url(${newAlbum.coverPath})` }">
		<!-- 状态栏占位 -->
		<view class="status-bar"></view>
		<view class="mask-layer" @tap.stop="hide" v-show="maskLayer"></view>

		<!-- #ifdef MP-WEIXIN -->
		<headBarWX title="修改歌单" :show='true' color='white'></headBarWX>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<headBarAPP title="修改歌单" :showButtonMore="false"></headBarAPP>
		<!-- #endif -->

		<view class="container">
			<view class="cover" @tap="chooseImage()">
				<image :src="newAlbum.coverPath" mode="widthFix"></image>
			</view>
			<view class="info">
				<view class="name">
					<text>名字:</text>
					<input v-model="newAlbum.name" />
				</view>
				<view class="desc">
					<text>描述:</text>
					<textarea v-model="newAlbum.desc"></textarea>
				</view>
				<view class="btn-private">
					<switch @change="switchChange_private" />
				</view>
			</view>
		</view>

		<!-- 悬浮按钮 -->
		<view class="btn-public" @tap="public()">
			<text class="iconfont">&#xe61d;</text>
		</view>
	</view>
</template>

<script>
	import headBarWX from '@/components/headBarWX/headBarWX.vue'
	import headBarAPP from '@/components/headBarAPP/headBarAPP.vue'
	// 导入云对象
	const cloudMusic = uniCloud.importObject('cloudMusic', {
		customUI: true // 取消自动展示的交互提示界面
	});
	const Album = uniCloud.importObject('Album', {
		customUI: true // 取消自动展示的交互提示界面
	});
	export default {
		data() {
			return {
				maskLayer: false,
				newAlbum: {
					coverPath: '',
					name: '',
					desc: '',
				},

				cover_modify: false,
			}
		},
		onLoad(options) {
			let albumObject = JSON.parse(options.albumObject);
			console.log('专辑信息：', albumObject);
			this.newAlbum = albumObject;
		},
		methods: {
			// #ifdef MP-WEIXIN
			chooseImage() {
				this.coverPath_temp =
					'https://mp-a52cf949-e385-4e78-b2b7-81a8dd621c31.cdn.bspapp.com/cloud/cover/women.jpg'
				console.log(this.coverPath_temp);
			},
			// #endif

			// #ifdef H5
			chooseImage() {
				console.log('选择封面');
				new Promise((resolve, reject) => {
					uni.chooseFile({
						count: 20,
						type: 'all',
						extension: ['.png', 'jpg'],
						success(res) {
							resolve(res);
						},
						fail(err) {
							reject(err);
						}
					});
				}).then(res => {
					this.newAlbum.coverPath = res.tempFilePaths[0];
					this.cover_modify = true;
				})
			},
			// #endif
			async public() {
				if (!this.newAlbum.name) {
					console.log('名字不能为空');
					return 0;
				}
				console.log('发布');
				console.log(this.newAlbum);

				uni.showLoading({
					title: '上传中...',
				})
				//1. 上传文件到云储存
				if (this.cover_modify) {
					let res = await uniCloud.uploadFile({
						cloudPathAsRealPath: true,
						cloudPath: "cloud/cover/" + (new Date()).getTime() + ".png",
						filePath: this.newAlbum.coverPath,
						onUploadProgress: function(progressEvent) {
							let percentCompleted = Math.round(
								(progressEvent.loaded * 100) / progressEvent.total
							);
							console.log(percentCompleted);
						}
					});
					let cloudFilePath = res.fileID;
					this.newAlbum.coverPath = cloudFilePath;
				}

				console.log('新的歌单信息：', this.newAlbum);

				Album.updateAlbumInfo(this.newAlbum).then(res => {
					console.log(res);
				});

				uni.hideLoading();

				uni.setStorageSync('updateAlbumInfo', true);
				uni.navigateBack();
			},

			switchChange_private(e) {
				const value = e.detail.value;
				this.newAlbum.private = value;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		position: relative;
	}

	.album-create::after {
		content: "";
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		height: 100vw;
		background-size: cover;
		background-image: var(--background-image, none);
		background-position: center;
		filter: blur(20px);
		z-index: -1;
	}

	.cover {
		margin: auto;
		margin-top: 80rpx;
		width: 340rpx;
		height: 340rpx;
		background-color: white;
		box-shadow: 0 0 20rpx 10rpx rgba(0, 0, 0, 0.1);
		border-radius: 20rpx;
		overflow: hidden;
		display: flex;
		justify-content: center;
		align-items: center;

		text {
			font-size: 200rpx;
			opacity: 0.4;
		}

		image {
			width: 100%;
			height: 100%;
		}
	}

	.info {
		margin-top: 80rpx;
		background-color: white;
		flex: 1;
		border-top-right-radius: 50rpx;
		border-top-left-radius: 50rpx;
		box-shadow: 0 0 20rpx 10rpx rgba(0, 0, 0, 0.1);

		.name {
			margin: auto;
			margin-top: 50rpx;
			width: 90%;

			text {
				font-size: $font-size-l-m;
			}

			input {
				margin-top: 20rpx;
				height: 100rpx;
				width: 100%;
				border: 5rpx solid rgba(0, 0, 0, 0.1);
				border-radius: 20rpx;
				box-sizing: border-box;
				padding: 20rpx;
				font-size: $font-size-l-m;
			}
		}

		.desc {
			width: 90%;
			margin: auto;
			margin-top: 30rpx;

			text {
				font-size: $font-size-l-m;
			}

			textarea {
				width: 100%;
				margin-top: 20rpx;
				min-height: 60rpx;
				border: 5rpx solid rgba(0, 0, 0, 0.1);
				border-radius: 20rpx;
				/* 禁止调整大小 */
				resize: none;
				box-sizing: border-box;
				padding: 20rpx;
			}
		}

		.btn-private {}
	}

	.btn-public {
		position: fixed;
		background-color: rgba(203, 116, 237, 0.5);
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		bottom: 40rpx;
		right: 30rpx;

		text {
			font-size: 60rpx;
		}
	}
</style>